﻿using Donal.Xms.Common;
using Newtonsoft.Json;
using System;
using System.Web.UI;

namespace Donal.Xms.Web.qianyi.dialog
{
    public partial class dialog_user_invoice_edit : Web.UI.ManagePage
    {
        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int user_id = 0;
        Model.company company = new Model.company();
        protected string area = string.Empty;
        private int invid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.user_id = DTRequest.GetQueryInt("id");
            this.invid = DTRequest.GetQueryInt("invid");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型

                if (this.user_id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.user_invoice().Exists(this.invid, this.user_id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.View.ToString()); //检查权限

                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    company = new BLL.company().GetModel(user_id);
                    if (company == null)
                    {
                        JscriptMsg("参数有误！", "back");
                        return;
                    }

                    ShowInfo(this.invid);
                }
                else if (action == DTEnums.ActionEnum.Add.ToString())
                {
                    company = new BLL.company().GetModel(user_id);
                    if (company == null)
                    {
                        JscriptMsg("参数有误！", "back");
                        return;
                    }
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.user_invoice bll = new BLL.user_invoice();
            Model.user_invoice model = bll.GetModel(_id);

            rblCategory.SelectedValue = model.category.ToString();
            txtCompany.Text = model.company;
            rblContenType.SelectedValue = model.content_type.ToString();
            txtRegistration.Text = model.registration;
            txtOpening_bank.Text = model.opening_bank;
            txtAccount_number.Text = model.account_number;
            rblDefault.SelectedValue = model.is_default.ToString();
            txtTelphone.Text = model.telphone;
            txtRegistered_address.Text = model.registered_address;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.user_invoice model = new Model.user_invoice();
            BLL.user_invoice bll = new BLL.user_invoice();

            model.category = int.Parse(rblCategory.SelectedValue);
            model.company = txtCompany.Text;
            model.content_type = int.Parse(rblContenType.SelectedValue);
            model.registration = txtRegistration.Text;
            model.opening_bank = txtOpening_bank.Text;
            model.account_number = txtAccount_number.Text;
            model.is_default = int.Parse(rblDefault.SelectedValue);
            model.user_id = user_id;
            model.telphone = txtTelphone.Text;
            model.registered_address = txtRegistered_address.Text;

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "为 " + "" + " 添加发票信息 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;

            BLL.user_invoice bll = new BLL.user_invoice();
            Model.user_invoice model = bll.GetModel(_id);

            model.category = int.Parse(rblCategory.SelectedValue);
            model.company = txtCompany.Text;
            model.content_type = int.Parse(rblContenType.SelectedValue);
            model.registration = txtRegistration.Text;
            model.opening_bank = txtOpening_bank.Text;
            model.account_number = txtAccount_number.Text;
            model.is_default = int.Parse(rblDefault.SelectedValue);
            model.user_id = user_id;
            model.telphone = txtTelphone.Text;
            model.registered_address = txtRegistered_address.Text;

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改 " + "" + " 发票信息信息 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(invid))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改发票信息成功！", "../common/company_edit.aspx?id=" + user_id + "&action=" + DTEnums.ActionEnum.Edit + "&pageindex=5");
            }
            else //添加
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加发票信息成功！", "../common/company_edit.aspx?id=" + user_id + "&action=" + DTEnums.ActionEnum.Edit + "&pageindex=5");
            }
        }


    }
}